package top.tupobi.zing;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.blankj.utilcode.util.ToastUtils;
import com.blankj.utilcode.util.Utils;

import top.tupobi.zing.network.beiyong.RetrofitManagerBeiyong;

public class ZingApplication extends Application {

    private static Context sApplicationContext;

    @Override
    public void onCreate() {
        super.onCreate();
        sApplicationContext = this;
        RetrofitManagerBeiyong.initOkHttpClient();
        Utils.init(this);

        addAppLifeCycle();
    }

    public static Context getAppContext() {
        return sApplicationContext;
    }

    private void addAppLifeCycle() {
        registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {

            @Override
            public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle savedInstanceState) {

            }

            @Override
            public void onActivityStarted(@NonNull Activity activity) {

            }

            @Override
            public void onActivityResumed(@NonNull Activity activity) {

            }

            @Override
            public void onActivityPaused(@NonNull Activity activity) {
            }

            @Override
            public void onActivityStopped(@NonNull Activity activity) {
            }

            @Override
            public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle outState) {

            }

            @Override
            public void onActivityDestroyed(@NonNull Activity activity) {

            }
        });
    }

    private static final String TAG = "tupobi";
    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        if (false) {
            Log.e("tupobi", "MyApplication attachBaseContext");
            new Handler(getMainLooper()).post(new Runnable() {
                @Override
                public void run() {
                    while (true) {
                        try {
                            Looper.loop();
                            //try-catch主线程的所有异常；Looper.loop()内部是一个死循环，出现异常时才会退出，所以这里使用while(true)。
                        } catch (Throwable e) {
                            Toast.makeText(sApplicationContext, "该功能出现未捕获的异常", Toast.LENGTH_SHORT).show();
                            Log.e(TAG, "主线程异常：" + e.getMessage());
                            e.printStackTrace();
                        }
                    }
                }
            });

            Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
                @Override
                public void uncaughtException(Thread t, Throwable e) {
                    //try-catch子线程的所有异常。
//                    Toast.makeText(sAppContext, "该功能有未捕获的异常", Toast.LENGTH_SHORT).show();
                    ToastUtils.showShort("该功能出现未捕获的异常");
                    Log.e(TAG, "子线程异常：" + e.getMessage());
                    e.printStackTrace();
                }
            });
        }
    }

}
